package main

func maxSubArray(nums []int) int {
	result := make([]int, len(nums))
	max := nums[0]
	for i := 0; i < len(nums); i++ {
		if i == 0 {
			result[0] = nums[0]
		} else {
			if result[i-1] > 0 {
				result[i] = result[i-1] + nums[i]
			} else {
				result[i] = nums[i]
			}
			if max < result[i] {
				max = result[i]
			}
		}
	}
	return max
}

func main() {
	result := []int{-2, 1, -3, 4, -1, 2, 1, -5, 4}
	println(maxSubArray(result))
}
